package ModelLayer;

import java.util.ArrayList;
import java.util.Date;

public class Sale {
	private int saleId;
	private Date date;
	private Date deliveryDate;
	private Enum deliverStatus;
	private Customer customer;
	private Invoice invoice;
	private double totalPrice;
	private ArrayList<SaleLine> saleLines;

	public Sale() {
		saleLines = new ArrayList<SaleLine>();
	}

	public Sale(int saleId) {
		this.saleId = saleId;
		saleLines = new ArrayList<SaleLine>();
	}

	public Sale(int saleId, Date date, Date deliveryDate, Enum deliverStatus,
			Customer customer) {
		this.saleId = saleId;
		this.date = date;
		this.customer = customer;
		saleLines = new ArrayList<SaleLine>();
	}

	public void calculateTotalPrice() {
		double totalPrice = 0;
		if (saleLines.size() != 0) {
			for (SaleLine sl : saleLines) {
				totalPrice = totalPrice + sl.getProduct().getSalePrice()
						* sl.getQuantity();
			}
		}
		setTotalPrice(totalPrice);
	}

	public void addSaleLine(Product product, int quantity) {
		SaleLine saleline = new SaleLine(this, product, quantity);
		saleLines.add(saleline);
		saleline.getProduct().setCurrentStock(
				saleline.getProduct().getCurrentStock() - quantity);
		calculateTotalPrice();
	}

	public void endSale() {

		if (invoice == null) {
			// TODO create invoice
		}

		if (deliveryDate != null) {
			invoice.setDelivery(true);
		}
		invoice.getSales().add(this);
		invoice.calculatePrice();
		// TODO update invoice
	}

	public int getSaleId() {
		return saleId;
	}

	public void setSaleId(int saleId) {
		this.saleId = saleId;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Date getDeliveryDate() {
		return deliveryDate;
	}

	public void setDeliveryDate(Date deliveryDate) {
		this.deliveryDate = deliveryDate;
	}

	public Enum getDeliverStatus() {
		return deliverStatus;
	}

	public void setDeliverStatus(Enum deliverStatus) {
		this.deliverStatus = deliverStatus;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Invoice getInvoice() {
		return invoice;
	}

	public void setInvoice(Invoice invoice) {
		this.invoice = invoice;
	}

	public ArrayList<SaleLine> getSaleLines() {
		return saleLines;
	}

	public void setTotalPrice(double totalPrice) {
		this.totalPrice = totalPrice;
	}

	public double getTotalPrice() {
		return totalPrice;
	}

	public void setSaleLines(ArrayList<SaleLine> saleLines) {
		this.saleLines = saleLines;
	}

}
